package cn.kgc.service;

import cn.kgc.entity.User;
import cn.kgc.mapper.UserMapper;
import cn.kgc.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/5/4
 */
@Service
public class UserServiceImpl implements  UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result login(User user) {
        User usr = userMapper.selectOne(user);
        Result<User> result = new Result<>();
        if (usr!=null){
            result.setCode(200);
            result.setMsg("登录成功");
            result.setData(usr);
            result.setCount(1);
        }else{
            result.setCode(500);
            result.setMsg("登录失败");
        }
        return result;
    }

    @Override
    public Result register(User user) {

        int i = userMapper.addUser(user);
        Result<User> result = new Result<>();
        if(i>0){
            result.setCode(200);
            result.setMsg("插入成功");
        }else{
            result.setCode(500);
            result.setMsg("插入失败");
        }

        return result;
    }

    @Override
    public Result findAll() {

        List<User> users = userMapper.selectAll();
        Result<List<User>> result = new Result<>();

        result.setCode(200);
        result.setMsg("查询成功");
        result.setData(users);
        result.setCount(users.size());

        return result;
    }
}
